//
//  ZH_CommoditymanageViewController.m
//  ChangeMerchants
//
//  Created by zhangliwen on 2018/9/21.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_CommoditymanageViewController.h"
#import "GC_SelectionCriteriaSegmentView.h"
#import "ZH_CommodityTableViewCell.h"
#import "Zh_GoodsInformationViewController.h"
#import "ZH_MallOrderViewModel.h"
@interface ZH_CommoditymanageViewController ()<UITableViewDelegate,UITableViewDataSource>
///选择条件 View
@property (nonatomic, weak) GC_SelectionCriteriaSegmentView *selectSegmentView;
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)UIImageView *lookImage;
@property (nonatomic,weak)UITextField *textField;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)ZH_MallOrderViewModel *viewModel;
@end

@implementation ZH_CommoditymanageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self loadTableView];
    [self loadDataForStart:1 isUpMore:NO];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
///加载列表数据
- (void)loadTableView{
    WEAKSelf;
    //上拉加载更多
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [weakSelf loadDataForStart:self.page isUpMore:YES];
    }];
    
    [footer setTitle:HenLocalizedString(@"没有更多的信息！") forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
}
-(void)loadDataForStart:(NSInteger)start isUpMore:(BOOL)isUpMore{
    [self.viewModel.goodsListParam setObject:self.idString forKey:@"id"];
    [self.viewModel.goodsListParam setObject:[NSString stringWithFormat:@"%ld",(long)start] forKey:@"page"];
    WEAKSelf;
    [self.viewModel goodsListResultBlock:^(NSString *code, NSString *message, id data) {
        [weakSelf hideHud];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        if(isUpMore){
            if(((NSMutableArray*)data).count < [NumberOfPages integerValue]){
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }

    }];
}
- (void)initUserInterface{
    self.navigationItem.titleView = self.bgView;
    self.view.backgroundColor = kCommonBackgroudColor;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(FITSCALE(316));
        make.height.mas_equalTo(FITSCALE(34));
    }];
    [self.lookImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(FITSCALE(70));
        make.width.mas_equalTo(FITSCALE(15));
        make.height.mas_equalTo(FITSCALE(15));
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView);
        make.left.equalTo(self.lookImage.mas_right).offset(FITSCALE(7));
        make.right.equalTo(self.bgView);
    }];
    [self.selectSegmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(HEIGHT_TRANSFORMATION(0));
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(HEIGHT_TRANSFORMATION(90));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.selectSegmentView.mas_bottom).offset(FITSCALE(9));
        make.left.equalTo(self.view).offset(FITSCALE(12));
        make.right.equalTo(self.view).offset(FITSCALE(-12));
        make.bottom.equalTo(self.view);
    }];
}
///输入框 回调 方法
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //    if(self.onEnterTextFieldBlock){
    //        self.onEnterTextFieldBlock(textField.text, textField.tag);
    //    }
    NSLog(@"%@",textField.text);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField endEditing:YES];
    return YES;
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.goodsListArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZH_CommodityTableViewCell *cell = [ZH_CommodityTableViewCell cellWithTableView:tableView];
    [cell updataUI:self.viewModel.goodsListArray[indexPath.row]];
    return cell;
//    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Zh_GoodsInformationViewController *vc  = [[Zh_GoodsInformationViewController alloc] init];
    vc.idString = [NSString stringWithFormat:@"%@",self.viewModel.goodsListArray[indexPath.row][@"id"]];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - prvate
///条件 当前选中项
- (void)customConditionSegmentCurrentItem:(NSUInteger) item andIsVariety:(BOOL)isVariety{
    if (item == 0) {
        [self.viewModel.goodsListParam setObject:@"" forKey:@"money"];
        [self loadDataForStart:1 isUpMore:NO];
    }
    else if (item == 1){
        if (isVariety) {
            [self.viewModel.goodsListParam setObject:@"0" forKey:@"money"];
            [self loadDataForStart:1 isUpMore:NO];
        }
        else{
            [self.viewModel.goodsListParam setObject:@"1" forKey:@"money"];
            [self loadDataForStart:1 isUpMore:NO];
        }
    }
}
///物品筛选 View
- (GC_SelectionCriteriaSegmentView *)selectSegmentView
{
    if(!_selectSegmentView){
        GC_SelectionCriteriaSegmentView *view = [[GC_SelectionCriteriaSegmentView alloc] initWithHeight:HEIGHT_TRANSFORMATION(90)];
        [view setTopLineImageViewHidden:YES];
        [view setBottomLineImageViewHidden:NO];
        view.updateNum = 2;
        [view setUnLineItems:@[@"综合",@"价格"]];
        WEAKSelf;
        //回调
        view.onSelectedBlock = ^(NSInteger item, BOOL isVariety) {
            [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
            [weakSelf customConditionSegmentCurrentItem:item andIsVariety:isVariety];
        };
        [view setSelectDefaultItem:0];
        [self.view addSubview:_selectSegmentView = view];
    }
    return _selectSegmentView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (UIView *)bgView{
    if (!_bgView) {
        UIView *view = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#CCEEEE"]];
        view.layer.cornerRadius = FITSCALE(18);
        view.layer.masksToBounds = YES;
        view.alpha = 0.9;
        view.clipsToBounds = YES;
        [self.view addSubview:_bgView = view];
    }
    return _bgView;
}
- (UIImageView *)lookImage{
    if (!_lookImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"icon_search"];
        [self.bgView addSubview:_lookImage = image];
    }
    return _lookImage;
}
- (UITextField *)textField
{
    if(!_textField){
        UITextField *textField = [UITextField createTextFieldWithPlaceholder:@"请输入请输入关键词" placeholderColor:kFontColorGray delegateTarget:self];
        textField.borderStyle = UITextBorderStyleNone;
        textField.font = kFontSize_28;
        textField.textAlignment = NSTextAlignmentLeft;
        textField.returnKeyType = UIReturnKeyDone;
        [textField setTextColor:kFontColorBlack];
        [self.bgView addSubview:_textField = textField];
    }
    return _textField;
}
- (ZH_MallOrderViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MallOrderViewModel alloc] init];
    }
    return _viewModel;
}
@end
